package engine.net.mgr
{
	import engine.data.ModelLocator;
	import engine.logic.login.LoginCallLater;
	import engine.net.NetSocket;
	import engine.net.datatype.ByteArrayEx;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.messages.SystemMsgFlag;
	
	import flash.geom.Point;
	
	import newx.utils.debug.IO;

	public class SystemSender
	{
		private static 	var _self	:SystemSender	=	null;
		
		private 			var net		:NetSocket		=	null;
		
		private			var	_account:String			=	"";		

		public				var UICompleteFunc:Function = null;
		public function SystemSender()
		{
			net	=	NetSocket.getInstance();
			
			//添加gm回复的回调
			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GM),function onFromGM(e:CustomMsgEvent):void
			{
				var		wsName:String	= e.pList[0];
				var		wsInfo:String	= e.pList[1];	
				IO.traceLog("wsInfo",wsInfo);
			});			
		}
		
		static public function getInstance():SystemSender
		{
			if(_self == null)
			{
				_self	=	new SystemSender();
			}
			return _self;
		}		
		
//		public function send(msg:int,...argc):void
//		{
//			//新建数据包
//			var sn_bytearray	:ByteArrayEx 	=	new ByteArrayEx();
//			
//			var pSize			:uint			=	argc.length;
//						
//			//写入消息号
//			sn_bytearray.writeShort(msg);
//			
//			switch(msg)
//			{
//				case SystemMsgFlag.CLIENT_LOGIN:
//					sn_bytearray.writeUnsignedInt(514);
//					break;
//				default:
//					break;
//			}
//			
//			ByteArrayEx.writeArray(sn_bytearray,argc);			
//			NetSocket.getInstance().send(sn_bytearray);	
//		}
		
		/**
		 * 登陆游戏
		 * @param account   账号
		 * @param password  密码
		 * @param RoleName  角色名 可选
		 */		
		public function Login(account:String, password:String, RoleName:String = ""):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_LOGIN);
			sn_bytearray.writeUnsignedInt(514);
			sn_bytearray.writeStringEx(account, false, 33);
			sn_bytearray.writeStringEx(password, false, 33);
			sn_bytearray.writeWidestrEx(RoleName, false, 66);
			sn_bytearray.writeStringEx("", false, 16);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_LOGIN("+SystemMsgFlag.CLIENT_LOGIN+")]," +
					"account:"+account+",password:"+password+",RoleName:"+RoleName);
			
			net.send(sn_bytearray);
			
			_account	=	account;
		}
		
		/**
		 * 串登录 
		 * @param account	：账号
		 * @param dataStr	：串
		 * 
		 */		
		public function LoginOtherWay(account:String,dataStr:String = ""):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			sn_bytearray.writeShort(SystemMsgFlag.CMSG_LOGIN_ANYWAY);
			//版本号
			sn_bytearray.writeUnsignedInt(514);
			//账户
			sn_bytearray.writeStringEx(account, false, 33);
			//密码
			sn_bytearray.writeStringEx("", false, 33);
			//角色名
			sn_bytearray.writeWidestrEx("", false, 66);
			//验证码
			sn_bytearray.writeStringEx("", false, 16);
			//登录串（牛盾）
			sn_bytearray.writeStringEx(dataStr, false, 301);
			//账户类型(1:普通，2:以验证串方式登录的帐户,4、niudun)
//			sn_bytearray.writeStringEx("2");
			sn_bytearray.writeByte(2);
			
			//客户端类型(0:普通，1:手机)
//			sn_bytearray.writeStringEx("0");
			sn_bytearray.writeByte(0);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CMSG_LOGIN_ANYWAY("+SystemMsgFlag.CMSG_LOGIN_ANYWAY+")]," +
					"SystemMsgFlag.CMSG_LOGIN_ANYWAY:"+SystemMsgFlag.CMSG_LOGIN_ANYWAY+
					",dataStr:"+dataStr);
			
			net.send(sn_bytearray);
			
			_account	=	account;
			
		}
		
		/**
		 *	 牛顿登陆
		 * @param account		账号
		 * @param pwd			密码
		 * @param dataStr		牛顿
		 * 
		 */
		public function LoginNiuDunWay(account:String, pwd : String, dataStr : String):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			sn_bytearray.writeShort(SystemMsgFlag.CMSG_LOGIN_ANYWAY);
			//版本号
			sn_bytearray.writeUnsignedInt(514);
			//账户
			sn_bytearray.writeStringEx(account, false, 33);
			//密码
			sn_bytearray.writeStringEx(pwd, false, 33);
			//角色名
			sn_bytearray.writeWidestrEx("", false, 66);
			//验证码
			sn_bytearray.writeStringEx("", false, 16);
			//登录串（牛盾）
			sn_bytearray.writeStringEx(dataStr, false, 301);
			//账户类型(1:普通，2:以验证串方式登录的帐户,4、niudun)
			//			sn_bytearray.writeStringEx("2");
			sn_bytearray.writeByte(4);
			
			//客户端类型(0:普通，1:手机)
			//			sn_bytearray.writeStringEx("0");
			sn_bytearray.writeByte(0);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CMSG_LOGIN_ANYWAY("+SystemMsgFlag.CMSG_LOGIN_ANYWAY+")]," +
					"SystemMsgFlag.CMSG_LOGIN_ANYWAY:"+SystemMsgFlag.CMSG_LOGIN_ANYWAY+
					",dataStr:"+dataStr);
			
			net.send(sn_bytearray);
			_account	=	account;
			
		}
		
		/**
		 * 
		 * @param strName	玩家名字
		 * @param nation	种族
		 * @param sex		性别	
		 * @param photo		头像		
		 * @param protype	职业
		 * 
		 */			
		public function CreatRole(strName:String,
									nation:int,
									sex:int,														
									photo:int,								
									protype:int,
									castleName:String):void
		{

//			* @param nation		种族
//			* @param sex		性别
//			* @param facemodel	脸部模型 
//			* @param hairstyle  发型
//			* @param photo		头像
//			* @param skincolor	肤色
//			* @param haircolor	发色
//			* @param protype	职业
			
			var	facemodel:int	=	0;
			var	hairstyle:int	=	0;
			var	skincolor:int	=	0;
			var	haircolor:int	=	0;
			
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_CREATE_ROLE);
//			sn_bytearray.WriteData(nation);
//			sn_bytearray.WriteData(protype);
//			sn_bytearray.WriteData(sex);
//			sn_bytearray.writeWidestrEx(strName, false, 66);
			sn_bytearray.writeByte(0);
			sn_bytearray.writeWidestrEx(strName, false, 66);			
			
			sn_bytearray.writeShort(9); //参数个数
			
			sn_bytearray.WriteData(nation);
			sn_bytearray.WriteData(protype);
			sn_bytearray.WriteData(sex);
			sn_bytearray.WriteData(facemodel);
			sn_bytearray.WriteData(hairstyle);
			sn_bytearray.WriteData(photo);
			sn_bytearray.WriteData(skincolor);
			sn_bytearray.WriteData(haircolor);
			sn_bytearray.WriteData(castleName);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_CREATE_ROLE("+SystemMsgFlag.CLIENT_CREATE_ROLE+")]," +
					"nation:"+nation+
					",sex:"+sex+
					",facemodel:"+facemodel+
					",hairstyle:"+hairstyle+
					",photo:"+photo+
					",skincolor:"+skincolor+
					",haircolor:"+haircolor+
					",protype:"+protype+
					",castleName:"+castleName);
			
			NetSocket.getInstance().send(sn_bytearray);
			
		}
		/**
		 * 选择角色 
		 * @param strName   角色名
		 * 
		 */		
		public function SelectRole(strName:String):void
		{
//			return;
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_CHOOSE_ROLE);
			
			var i:uint = 0;
			for (i = 0; i < 16; i++)
			{
				sn_bytearray.writeByte(0);
			}
			sn_bytearray.writeStringEx(_account, false, 33);
			sn_bytearray.writeWidestrEx(strName, false, 66);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_CHOOSE_ROLE("+SystemMsgFlag.CLIENT_CHOOSE_ROLE+")]," +
					"_account:"+_account+
					",strName:"+strName);
			
			if(LoginCallLater.AllSwfReady)
			{
				NetSocket.getInstance().send(sn_bytearray);
			}
			else
			{
				UICompleteFunc	=	function():void
				{
					NetSocket.getInstance().send(sn_bytearray);
				}
			}
			
//			NetSocket.getInstance().send(sn_bytearray);
		}
		
		/**
		 * 发Gm求助消息 
		 * @param str
		 * 
		 */		
		public function SendGMCCTOServer(str:String):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_TO_GM);
			//sn_bytearray.writeStringEx(_account, false, 33);			
			sn_bytearray.writeWStringEx(str);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_TO_GM("+SystemMsgFlag.CLIENT_TO_GM+")]," +
					"str:"+str);
			
			NetSocket.getInstance().send(sn_bytearray);
		}
		/**
		 * 删除角色 
		 * @param strName  角色名字
		 * 
		 */		
		public function DelRole(strName:String):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_DELETE_ROLE);
			sn_bytearray.writeWidestrEx(strName, false, 66);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_DELETE_ROLE("+SystemMsgFlag.CLIENT_DELETE_ROLE+")]," +
					"strName:"+strName);
			
			NetSocket.getInstance().send(sn_bytearray);
		}

		/**
		 * 选中菜单 
		 * @param uniID 对象的唯一ID
		 * @param asyID
		 * @param mark 标记
		 * 
		 */		
		public function SelectMenu(uniID:Point, asyID:Point, mark:int):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_SELECTMENU);
			sn_bytearray.writeObjectEx(uniID);
			sn_bytearray.writeObjectEx(asyID);
			sn_bytearray.writeShort(mark);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_SELECTMENU("+SystemMsgFlag.CLIENT_SELECTMENU+")]," +
					"uniID:"+uniID+
					",asyID:"+asyID+
					",mark:"+mark);
			
			NetSocket.getInstance().send(sn_bytearray);
		}
		/**
		 * 选中一个 NPC 
		 * @param uinID 对象的唯一ID
		 * @param func 功能号
		 * 
		 */		
		public function SelectObjInScene(uinID:Point, func:int = 0):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_SELECT);
			var i:uint = 0;
			for (i = 0; i < 16; i++)
			{
				sn_bytearray.writeByte(0);
			}
			sn_bytearray.writeInt(NetSocket.serialNum);
			NetSocket.serialNum++;
			sn_bytearray.writeObjectEx(uinID);
			sn_bytearray.writeShort(func);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_SELECT("+SystemMsgFlag.CLIENT_SELECT+")]," +
					"uniID:("+uinID.x+","+uinID.y+
					"),func:"+func);
			
			NetSocket.getInstance().send(sn_bytearray);
		}
		/**
		 * 客户端准备就绪 
		 * 
		 */	    
		public function ClientReady():void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_READY);
			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送引擎消息[CLIENT_READY("+SystemMsgFlag.CLIENT_READY+")]");
			
//			if(LoginCallLater.AllSwfReady)
//			{
				NetSocket.getInstance().send(sn_bytearray);
//			}
//			else
//			{
//				UICompleteFunc	=	function():void
//				{
//					NetSocket.getInstance().send(sn_bytearray);
//				}
//			}
		}
		
		public function CustomMsgEx(command:int,plist:Array):void
		{
			
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			sn_bytearray.writeShort(SystemMsgFlag.CLIENT_CUSTOM);
			
			//			_UI08		nVerify[16];	// 校验码
			//			_UI32		nSerial;		// 消息序列号	
			//			ASYNC_ID	AsyncId;		// 异步事件标识
			//			_UI16		nArgNum;		// 参数数量
			//			//_UI08		nArgType;		// 参数类型
			//			// 参数数据
			
			var i:uint = 0;
			for (i = 0; i < 16; i++)
			{
				sn_bytearray.writeByte(0);
			}			
			sn_bytearray.writeInt(NetSocket.serialNum);
			NetSocket.serialNum++;
			
			var	asyID:Point		= new Point();		
			asyID.x	=	0;
			asyID.y	=	0;
			sn_bytearray.writeObjectEx(asyID);
			
			sn_bytearray.writeShort(plist.length + 1);	
			
			sn_bytearray.WriteData(command);
			var	j:uint	=	0;
			for(j = 0;	j < plist.length; j++)
			{
				sn_bytearray.WriteData(plist[j]);
			}			
			if(SystemReturn._isShowLog)
				IO.traceCommand("    ↑		发送自定义消息["+command+"],参数："+plist);
			NetSocket.getInstance().send(sn_bytearray);
		}
		
		public function sendTest(val:String):void
		{
			var sn_bytearray:ByteArrayEx = new ByteArrayEx();
			
			sn_bytearray.writeMultiByteEx(val);
			
			NetSocket.getInstance().send(sn_bytearray);
		}
		
		/**
		 * 发送自定义消息 
		 * 
		 */
		public function CustomMsg(command:int,...argc):void
		{
			CustomMsgEx(command,argc)
		}	
		
		/**
		 * 发送GM命令 
		 * @param content 发送内容
		 * 
		 */
		public function SendGMCommand(content:String):void
		{
			CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GM,new WString(content));
		}		
	}
}